import {solarPowerSystem} from "../../common/projectAlias";
import { loginApi } from "@/http/login"; 
import store from '@/store'
import router from '@/router'
import  type {MutationTree,ActionTree,GetterTree}  from 'vuex'
import  type  {State}  from '../index'
import { PlainObject } from "@/interface/base"; 
import { inject } from "vue";

export interface MessageModel extends PlainObject {
    userName: string,
    loginName: string,
    mobile: string,
}
export interface ListModel<T> extends PlainObject {
    jumpType: '',
    projectList: T
}
export  interface UserState{
    message: MessageModel,
    menuMap: string[],
    portalList:ListModel<string[]>,
}
let state:UserState = {
    message: {
        userName: '',
        loginName: '',
        mobile: '',
    },
    menuMap: [],
    portalList:{
        jumpType: '',
        projectList: []
    },
};
//<UserState,State>//参数1是为了访问当前模块下的state的变量  参数2是引入的主模块下的
//不知道类型注解几个参数看上图
let getters:GetterTree<UserState,State> = {
    getProjectList(newState) {
        return newState.portalList;
    },

    //把配置页面的返回做了不返回的定制限制
    getMenuList(newState) {
        let data = [];
        const menuList  = JSON.parse(sessionStorage.menuList||null)
        if(newState.menuMap.length){
            data = newState.menuMap
        }
        if(menuList.length){
            data = menuList
        }
        return data
        // return newState.menuMap||menuList||[];
    },

    //配置页面的动态菜单
    getConfigMenu(newState){
        let data = [];
        const menuList  = JSON.parse(sessionStorage.menuList||null)
        if(newState.menuMap.length){
            data = newState.menuMap
        }
        if(menuList.length){
            data = menuList
        }
        return data
    },

    getUserName(newState){
            let name = sessionStorage.userName || '';
            return newState.message.userName === '' ? name : newState.message.userName
    }
};
let mutations:MutationTree<UserState> = {
    SET_Message(newState, mes) {
        let {message, menuMap, portalList} = mes
        newState.message = message;
        newState.menuMap = menuMap;
        newState.portalList = portalList;
        sessionStorage.userName = message.userName;//登录用户
        //存储列表数据及登录数据
        sessionStorage.setItem('menuList',JSON.stringify(menuMap||[]))
        store.commit('userMessage/updateRouteList',menuMap)
    },
    //拼接所有子路由
    updateRouteList(newState,menuMapList){
        const menuList  = JSON.parse((sessionStorage.menuList||null))
        var menuMap = []
        if( menuMapList?.length){
            menuMap = menuMapList
        }
        if( menuList?.length){
            menuMap = menuList
        }
        //存储子路由的列表
        var menuChildList: any[] = []
        menuMap.map(menu => {
            if (menu.childList) {
                menuChildList.push(...menu.childList)
            }else if (menu.url !== ''){
                menuChildList.push(menu)
            }
        })

        //进行路由拼接
        const routeList = {
            path: '/',
            name: 'index',
            component: () => import('@/common/index/index.vue'),
            children: menuChildList.map(menu=>{
                return {
                    path:'/'+menu.url,
                    name:menu.url,
                    meta:{
                        title:menu.menuName,
                        menuCode:menu.menuCode
                    },
                    component:()=>import(`../../views/${menu.url}/index.vue`)
                }
            })
        }
        //注册路由
        router.addRoute(routeList)
    }
};
let actions:ActionTree<UserState,State> = {
    setMessage({commit}, mes) {
        let message = {
            userName: mes.userName,
            loginName: mes.loginName,
            mobile: mes.mobile,
        }
        let menuMap =  mes.menuMap[solarPowerSystem];
        let portalList = {
            jumpType: mes.jumpType,
            projectList: mes.projectList,
        };
        return commit('SET_Message', {message,menuMap,portalList})
    },
    //获取用户信息，用于其他系统跳转
    async getLoginUser(){
        // const res =  await api.getLoginFun('login/getLoginUser')
        const res = loginApi.getLoginUser()
        store.dispatch('userMessage/setMessage',res)
    }
};
export default {
  namespaced: true,//开始模块命名空间
  state,
  getters,
  mutations,
  actions,
};